package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.DistributorAreaDao;
import com.xbongbong.saas.domain.entity.DistributorAreaEntity;
import com.xbongbong.saas.model.DistributorAreaModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bomfish
 */
@Service("distributorAreaModel")
public class DistributorAreaModelImpl implements DistributorAreaModel {
    @Resource
    private DistributorAreaDao distributorAreaDao;

    /**
     * 列表只需取出核心字段
     */
    private static final String LIST_COLUMNS = null;

    @Override
    public Long insert(DistributorAreaEntity entity) {
        return distributorAreaDao.insert(entity);
    }

    @Override
    public void insertBatch(List<DistributorAreaEntity> list) {
        distributorAreaDao.insertBatch(list);
    }

    @Override
    public List<DistributorAreaEntity> findEntities(Map<String, Object> param) {
        return distributorAreaDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return distributorAreaDao.getEntitiesCount(param);
    }

    @Override
    public void update(DistributorAreaEntity entity) {
        distributorAreaDao.update(entity);
    }

    @Override
    public Integer deleteBatch(List<Long> idIn, String corpid) throws XbbException {
        return  distributorAreaDao.deleteBatch(idIn, corpid);
    }

    @Override
    public DistributorAreaEntity getByKey(Long key, String corpid) {
        return distributorAreaDao.getByKey(key, corpid);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return distributorAreaDao.deleteByKey(key,corpid);
    }

    @Override
    public List<DistributorAreaEntity> findAllDistributorArea(String corpid) {
        Map<String,Object> param = new HashMap<String,Object>(8){{
            put("corpid",corpid);
            put("del",0);
            put("orderByStr","sort,update_time asc");
        }};
        return findEntities(param);
    }

    @Override
    public Integer updateBatch(List<DistributorAreaEntity> list, String corpid) {
        return distributorAreaDao.updateBatch(list, corpid);
    }


    @Override
    public List<DistributorAreaEntity> getSubAreaList(String corpid, Collection<Long> areaIdIn) {
        List<DistributorAreaEntity> list = new ArrayList<>();
        if (areaIdIn == null || areaIdIn.size() == 0) {
            return list;
        }
        // 设置参数 主要增加corpid参数和routerRegexp参数，以及将 negDel 置为1
        Map<String, Object> param = formatSubAreaListParam(corpid, areaIdIn);

        Integer start = 0;
        //步长
        Integer pageNum = 10000;
        /************查询指标************/
        for(;start <= Integer.MAX_VALUE; start = start + pageNum){
            param.put("start", start);
            param.put("pageNum", pageNum);
            List<DistributorAreaEntity> perList = findEntities(param);
            list.addAll(perList);
            //查询出的结果少于步长，肯定是没有数据了
            if (perList.size() < pageNum) {
                break;
            }
        }
        return list;
    }

    /**
     * 用于getSubAreaList 及 getSubAreaIdList 构造参数的方法
     * 主要增加corpid参数和routerRegexp参数
     * @param corpid 公司id
     * @param areaIdIn 要获取子区域ids的所有区域ids
     * @return 增加
     */
    private Map<String, Object> formatSubAreaListParam(String corpid, Collection<Long> areaIdIn) {
        StringBuilder areaIdInRegexp = new StringBuilder();
        int i = 0;
        for (Long areaId : areaIdIn) {
            areaIdInRegexp.append("(\\|").append(areaId).append("\\|)");
            if (i != areaIdIn.size() - 1) {
                areaIdInRegexp.append("|");
            }
            i++;
        }
        Map<String, Object> param = new HashMap<String, Object>(PaasConstant.DEFAULT_INITIAL_CAPACITY){{
            put("del", 0);
            put("corpid", corpid);
            /*
             * 数据库内dep_id_router字段的数值格式为：|1|2|4|
             * 如果想获取部门id为1、2的部门的所有子部门，则等于获取dep_id_router所有包含"|1|"或"|2|"的
             */
            put("routerRegexp", areaIdInRegexp.toString());
        }};
        return  param;
    }
}
